# vim: set ft=sh:
# 
# live1 -  live 'hook' for mkinitcpio: set up tmpfs and find boot device
#
# Author: xiooli <xioooli@yahoo.com.cn> <http://joolix.com>
#
# This sctipt is part of joolix live OS, it's based on Michael Towers 
# (gradgrind) <mt.42@web.de>'s larch1 script.
#
#    joolix is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    joolix is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with joolix; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#----------------------------------------------------------------------------
# 2009.8.7

DISTRO=JOOLIX
sgnfile="${sgnfile:-"$DISTRO/livecd.sgn"}"

# Replacement for msg which includes leading and trailing spaces
msg_ () 
{
	if [ "${quiet}" != "y" ]; then
		if [ "${@#::}" != "$@" ];then
			echo "[0;1m""$@""[0;0m"
		elif [ "${@# !}" != "$@" ];then
			echo "\033[0;31m"$@"\033[0;0;1m"
		elif [ "${@# :}" != "$@" ];then
			echo "\033[0;32m"$@"\033[0;0;1m"
		else
			echo "$@"
		fi
	fi
}

# Try to mount a disk, partition or cdrom
# $1 = device name (e.g. "/dev/hda2")
# $2 = directory where devices will be mounted
mount_dev ()
{
	/bin/mount -t vfat -o noatime,rw "$1" "$2" || \
	/bin/mount -r -t iso9660 "$1" "$2"		   || \
	/bin/mount -t ntfs -o noatime,rw "$1" "$2" || \
	/bin/mount -t ext4 -o noatime,rw "$1" "$2" || \
	/bin/mount -t ext3 -o noatime,rw "$1" "$2" || \
	/bin/mount -t ext2 -o noatime,rw "$1" "$2"
}

# klibc dose not support basename
basename () { echo "${1##*/}"; }

# Try to mount a disk, partition or cdrom and look for the sgnfile
# in the JOOLIX directory.
# If live system found in the device, return 0, else return 1
# and leave the device mounted.
# $1 = device name (e.g. "/dev/hda2")
# $2 = directory where devices will be mounted
test_live_data_dir ()
{
	# searching for the data device which contains squashed modules
	if [ -e "$1" ]; then 
		mount_dev "$1" "$2" 1>/dev/null 2>/dev/null
		if [ $? -eq 0 ]; then
			if [ -f "$2/$sgnfile" ]; then
				LDEV="$1"
				msg_ " : ==> live data found at $1"
				return 0
			else
				/bin/umount $2 2>/dev/null
				LDEV=""
				msg_ " ! ==> $1 mounted, but no $(basename $sgnfile) found"
				return 1
			fi
		else
			LDEV=""
			msg_ " ! ==> can not mount $1"
			return 1
		fi
	else
		LDEV=""
		return 1
	fi
}

run_hook ()
{
    msg_ ":: Creating writeable filesystem (tmpfs)"
    /bin/mkdir "/tfs"
    # Boot option copy-to-ram (c2r)
    if [ "${c2r}" = "y" ]; then
        TFSSIZE="80%"
    else
        TFSSIZE="60%"
    fi
    /bin/mount -t tmpfs -o "size=${TFSSIZE}" tmpfs "/tfs"

    # Directory for test mounts (and then for live CD)
    cdmount="/medium"
    /bin/mkdir -p "${cdmount}"

    # look for livecd data directory
	msg_ ":: Looking for ${sgnfile} in storage devices ..."
	
	# first try ${root}
	LDEV="${root}"
    if [ "x${LDEV}" != "x" ]; then
		msg_ ":: Searching in ${root} ..."
        if [ "x${usbdelay}" != "x" ]; then /bin/sleep ${usbdelay}; fi
		test_live_data_dir "${root}" "${cdmount}"
	fi

    #  then, try cdroms
    if [ "x${LDEV}" = "x" ] && [ "x${nocd}" = "x" ]; then
        msg_ ":: Searching in cdrom device ..."
        cdroms=$( /bin/cat /proc/sys/dev/cdrom/info | { while read a b c; do
                if [ "${a}" = "drive" -a "${b}" = "name:" ]; then
                    echo "${c}"
                    break
                fi
            done
        } )
        for i in ${cdroms}; do
            test_live_data_dir "/dev/${i}" "${cdmount}"
			if [ $? -eq 0 ]; then break; fi
        done
    fi
	
	# finally, test USB devices (and disks) repeatedly until timed out
    if [ "x${LDEV}" = "x" ]; then
        msg_ ":: Searching in usb (and disk) devices ..."
		for i in 1 2 3; do
			msg_ "wait $i..."
			/bin/sleep "$i"
			for d in /dev/sd[a-z][0-9]*; do
				if [ "$(replace "${d}" "[")" != "${d}" ]; then
					msg_ " ! ==> no usb/disk device found"
					break 2
				fi
				test_live_data_dir "${d}" "${cdmount}"
				if [ $? -eq 0 ]; then break 2; fi
			done
		done
    fi
}
